
package com.michael.ems.controller;

import com.michael.ems.controller.req.AddSystemReq;
import com.michael.ems.controller.req.ModifySystemReq;
import com.michael.ems.controller.req.QuerySystemCondition;
import com.michael.ems.controller.vo.SystemVO;
import com.michael.ems.model.QSystem;
import com.michael.io.*;
import com.michael.plugins.PageHelper;
import com.querydsl.core.types.Projections;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.SQLQueryFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "系统管理模块")
@RestController
@RequestMapping(value = "/sys")
public class SystemController {
    @Autowired
    private SQLQueryFactory sqlQueryFactory;
    QSystem system = QSystem.system;

    @ApiOperation(value = "查看系统")
    @RequestMapping(value = "/system/view", method = RequestMethod.POST)
    public RespData<SystemVO> addSystem(@RequestParam("sysId") Integer sysId) {
        SystemVO systemVO = sqlQueryFactory.select(Projections.bean(SystemVO.class,
                system.id, system.name, system, system.code, system.desc,
                system.manager, system.developer,
                system.createTime, system.creator, system.modifer, system.modifyTime))
                .from(system)
                .where(system.id.eq(sysId))
                .fetchOne();
        RespData<SystemVO> result = new RespData<SystemVO>();
        result.setBody(systemVO);
        return result;
    }

    @ApiOperation(value = "查看系统")
    @RequestMapping(value = "/system/query", method = RequestMethod.POST)
    public RespPageBody<SystemVO> querySystem(
            @RequestBody ReqPage<QuerySystemCondition> req) {
        QuerySystemCondition condition = req.getCondition();
        SQLQuery<SystemVO> sqlQuery = sqlQueryFactory.select(Projections.bean(SystemVO.class,
                system.id, system.name, system, system.code, system.desc,
                system.manager, system.developer,
                system.createTime, system.creator, system.modifer, system.modifyTime))
                .from(system);
        if (StringUtils.isNotBlank(condition.getName())) {
            sqlQuery.where(system.name.like(condition.getName()));
        }
        PageHelper.buildPage(req.getPage());
        PageHelper.execute(sqlQuery);
        return PageHelper.getResp();
    }

    @ApiOperation(value = "新增系统")
    @RequestMapping(value = "/system/add", method = RequestMethod.POST)
    public RespStatus addSystem(@RequestBody AddSystemReq req) {
        sqlQueryFactory.insert(system)
                .set(system.name, req.getName())
                .set(system.code, req.getCode())
                .set(system.desc, req.getDesc())
                .set(system.manager, req.getManager())
                .set(system.developer, req.getDeveloper())
                .execute();
        return new RespStatus();
    }

    @ApiOperation(value = "修改系统")
    @RequestMapping(value = "/system/modify", method = RequestMethod.POST)
    public RespStatus modifySystem(@RequestBody ModifySystemReq req) {
        sqlQueryFactory.update(system)
                .set(system.name, req.getName())
                .set(system.code, req.getCode())
                .set(system.desc, req.getDesc())
                .set(system.manager, req.getManager())
                .set(system.developer, req.getDeveloper())
                .where(system.id.eq(req.getId()))
                .execute();
        return new RespStatus();
    }

    @ApiOperation(value = "删除系统")
    @RequestMapping(value = "/system/delete", method = RequestMethod.DELETE)
    public RespStatus deleteSytemm(@RequestParam("sysId") Integer sysId) {
        sqlQueryFactory.delete(system)
                .where(system.id.eq(sysId))
                .execute();
        return new RespStatus();
    }
}
